package com.kxmall.huishou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxmall.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kxmall.huishou.domain.bo.HsUserTaskBo;
import com.kxmall.huishou.domain.vo.HsUserTaskVo;
import com.kxmall.huishou.domain.HsUserTask;
import com.kxmall.huishou.mapper.HsUserTaskMapper;
import com.kxmall.huishou.service.IHsUserTaskService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户任务信息Service业务层处理
 *
 * @author kxmall
 * @date 2023-11-23
 */
@RequiredArgsConstructor
@Service
public class HsUserTaskServiceImpl implements IHsUserTaskService {

    private final HsUserTaskMapper baseMapper;

    /**
     * 查询用户任务信息
     */
    @Override
    public HsUserTaskVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户任务信息列表
     */
    @Override
    public TableDataInfo<HsUserTaskVo> queryPageList(HsUserTaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HsUserTask> lqw = buildQueryWrapper(bo);
        Page<HsUserTaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户任务信息列表
     */
    @Override
    public List<HsUserTaskVo> queryList(HsUserTaskBo bo) {
        LambdaQueryWrapper<HsUserTask> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HsUserTask> buildQueryWrapper(HsUserTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HsUserTask> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), HsUserTask::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), HsUserTask::getContent, bo.getContent());
        lqw.eq(bo.getType() != null, HsUserTask::getType, bo.getType());
        lqw.eq(bo.getCountType() != null, HsUserTask::getCountType, bo.getCountType());
        return lqw;
    }

    /**
     * 新增用户任务信息
     */
    @Override
    public Boolean insertByBo(HsUserTaskBo bo) {
        HsUserTask add = BeanUtil.toBean(bo, HsUserTask.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户任务信息
     */
    @Override
    public Boolean updateByBo(HsUserTaskBo bo) {
        HsUserTask update = BeanUtil.toBean(bo, HsUserTask.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HsUserTask entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户任务信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
//        HsUserTask hsUserTask = new HsUserTask();
//        hsUserTask.setDelFlag(1l);
//        int num = baseMapper.update(hsUserTask, new QueryWrapper<HsUserTask>().in("id", ids));

        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
